﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventStandsApp
{
    public class EventStand
    {
        /*Fields*/
        private int standNo;
        private List<Order> orders;
        private Client client;
        private List<RentedItem> items;

        DataHelper dh;
        private static long orderNo;

        //Constructor
        public EventStand(int standNo,int clientId,DataHelper d)
        {
            this.dh = d;
            this.standNo = standNo;
            client = dh.getClient(clientId);
            if (client != null)
            {
                items = dh.loadAllRentedItems();
                orders = new List<Order>();
                orderNo = dh.getMaxOrderNo() + 1;
                List<Order> temp = dh.getOrders(client);
                if (temp != null)
                {
                    for (int i = 0; i < temp.Count; i++)
                    {
                        orders.Add(temp[i]);
                    }
                    for (int i = 0; i < orders.Count; i++)
                    {
                        List<RentedItem> r = dh.loadRentedItemsFromOrder(orders[i].OrderNo);
                        for (int j = 0; j < r.Count; j++)
                        {
                            orders[i].addItem(r[j]);
                        }
                    }
                    for (int i = 0; i < orders.Count; i++)
                    {
                        orders[i].addAllOrdersLine();
                    }
                }
            }
        }

        //Getters and Setters
        public int StandNo
        {
            get{return standNo;}
        }
        public List<RentedItem> getAllItems()
        {
            return items;
        }

        //returns an object of type client based on the client's id
        public Client getClient(int clientNo)
        {
            if (client != null)
            {
                client = dh.getClient(clientNo);
                client.reloadRentedItems();
            }
            return client;
        }

        /*returns an object of type Order based on client id and product id
         * if no such order is found it returns null
         * */
        public Order getClientOrder(int clientId, int prodId)
        {
            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].getClientId() == clientId && orders[i].findProdId(prodId) == true)
                {
                    return orders[i];
                }
            }
            return null;
        }

        /*
         * returns a list of orders for one client
         * if no orders will be found, an empty list will be returned*/
        public List<Order> getAllClientOrders()
        {
            List<Order> temp = new List<Order>();
            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].getClientId() == client.ClientId)
                {
                    temp.Add(orders[i]);
                }

            }
            return temp;
        }

        /*
         * updates the no of units in stock by increasing with 1 for a certain item
         * */
        public void updateUnitsInStock(RentedItem it)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ProductId == it.ProductId)
                {
                    items[i].UnitsInStock++;
                    dh.updateNoInStock(it.ProductId, items[i].UnitsInStock);
                }
            }
        }

        /*
         * updates the no of units in stock  with 100 for a certain item
         * returns true if the item was found, false otherwise
         * */
        public bool redoStock(int prodId)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ProductId == prodId)
                {
                    items[i].UnitsInStock = 100;
                    dh.updateNoInStock(prodId, items[i].UnitsInStock);
                    return true;
                }
            }
            return false;
        }

        /*
         * updates the amount of money spent of a client with a certain amount
         * */
        public void lowerMoneySpent(Client c,double amount)
        {
            dh.lowerMoneySpent(c.ClientId, amount);
        }

        /*
         * adds a new order into the list
         * */
        public void addOrder(Client c,int days,double price,List<int> prodIds)
        {
            Order o = new Order(orderNo, DateTime.Now,c, days, price);
            orderNo++;
            orders.Add(o);
            o.processOrder(standNo);
            for (int i = 0; i < prodIds.Count; i++)
            {
                RentedItem r = getItem(prodIds[i]);
                for (int j = 0; j < r.HowManyInOrder; j++)
                {
                    o.addOrderLine(prodIds[i]);
                }
                
            }
            dh.updateBalace(c.ClientId, c.Balance);
            dh.updateMoneySpent(c.ClientId, price);
            for (int j = 0; j < items.Count; j++)
            {
                dh.updateNoInStock(items[j].ProductId, items[j].UnitsInStock);
            }
        }

        /*
         * returns an item based on its id
         * if no such id is found it will return null
         * */
        public RentedItem getItem(int prodId)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ProductId == prodId)
                {
                    return items[i];
                }
            }
            return null;
        }

        /*
         * updates the no of units in stock by increasing with 1 for a certain item
         * */
        public void plusStock(int prodId)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ProductId == prodId)
                {
                    items[i].UnitsInStock++;
                }
            }
        }
    }
}
